//
//  CommonAddressViewController.m
//  didacar
//
//  Created by Yang Yuexia on 15/12/10.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "CommonAddressViewController.h"
#import "CommonAddressCell.h"
#import "UIButton+Dzm.h"
#import "SelectLocationViewController.h"
#import "CommonAddressViewModel.h"
#import "DatePickerViewController.h"
#import "UIViewController+WtPrestent.h"

@interface CommonAddressViewController ()<UITableViewDataSource,UITableViewDelegate,SelectLocationDelegate>

@property (strong, nonatomic) CommonAddressViewModel *commonAddressViewModel;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
- (IBAction)saveAction:(id)sender;

@end

@implementation CommonAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"常用地址";
    
    self.commonAddressViewModel = [[CommonAddressViewModel alloc ] init];
   
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self setLeftBtnItem];
    
    [self.saveButton styleButton];
    
    
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyAddress"];
    
     // 家庭地址
    NSString *homeAddress = [dic objectForKey:@"MyHomeAddress"];
    if (homeAddress == nil || ![homeAddress isEmptyOrWhitespace]) {
        self.commonAddressViewModel.homePoisModel.address = homeAddress;
        self.commonAddressViewModel.homePoisModel.name = homeAddress;
        // 家庭地址经度
        self.commonAddressViewModel.homePoisModel.latitude = [[dic objectForKey:@"MyHomeLat"] doubleValue];
        
        self.commonAddressViewModel.homePoisModel.longitude = [[dic objectForKey:@"MyHomeLng"] doubleValue];
    }
    
    
    NSString *worKAddress = [dic objectForKey:@"MyWorkAddress"];
    if (worKAddress == nil || ![worKAddress isEmptyOrWhitespace]) {
        
        self.commonAddressViewModel.companyPoisModel.address = worKAddress;
        self.commonAddressViewModel.companyPoisModel.name = worKAddress;
        // 家庭地址经度
        self.commonAddressViewModel.companyPoisModel.latitude = [[dic objectForKey:@"MyWorkLat"] doubleValue];
        
        self.commonAddressViewModel.companyPoisModel.longitude = [[dic objectForKey:@"MyWorkLng"] doubleValue];
     
    }
    
    NSString *startTime = [dic objectForKey:@"MyGoWorkTime"];
    if (startTime != nil && ![startTime isEmptyOrWhitespace]) {
        self.commonAddressViewModel.startTime = startTime;
    }


    NSString *endTime = [dic objectForKey:@"MyOffWorkTime"];
    if (endTime != nil && ![endTime isEmptyOrWhitespace]) {
        self.commonAddressViewModel.endTime = endTime;
    }

    [self.tableView reloadData];
    
}

#pragma mark ---- 列表代理 ---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.commonAddressViewModel.titleList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.commonAddressViewModel.titleList[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 10)];
    vi.backgroundColor = [UIColor clearColor];
    return vi;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommonAddressCell"];
    cell.title.text = self.commonAddressViewModel.titleList[indexPath.section][indexPath.row];
    
    // 地址
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.content.text = self.commonAddressViewModel.homePoisModel.name;
        }else{
            cell.content.text = self.commonAddressViewModel.companyPoisModel.name;
        }
    }
    // 时间
    else{
        if (indexPath.row == 0) {
            cell.content.text = self.commonAddressViewModel.startTime;
        }else{
            cell.content.text = self.commonAddressViewModel.endTime;
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.commonAddressViewModel.addressTimeTag = [[NSString stringWithFormat:@"%ld%ld",indexPath.section,indexPath.row] integerValue];
    // 住址
    if (indexPath.section == 0) {
        [self presentLocation];
    }
    // 时间
    else{
        [self showDatePicker];
    }
}

-(void)presentLocation{
    UINavigationController *cityNavigation = (UINavigationController *)[BaseViewController getInitialViewControllerFromSbName:@"SelectLocation"];
    SelectLocationViewController *selecetdLocation = cityNavigation.viewControllers[0];
    selecetdLocation.delegate = self;
    selecetdLocation.isHiddenCommoneAddress = YES;
    [self presentViewController:cityNavigation animated:YES completion:^{
        
    }];
}

-(void)selectedPoiModel:(PoisModel *)poisModel{
    if (self.commonAddressViewModel.addressTimeTag == 0) {
        self.commonAddressViewModel.homePoisModel = poisModel;
    }else{
        self.commonAddressViewModel.companyPoisModel = poisModel;
    }
    [self.tableView reloadData];
}


/**
 *  选择时间
 *
 *  @param text 之前的时间
 */
-(void)showDatePicker
{
    DatePickerViewController *vcDate = [[UIStoryboard storyboardWithName:@"StartCustomMade" bundle:nil]  instantiateViewControllerWithIdentifier:@"DatePickerViewController"];
    if (self.commonAddressViewModel.addressTimeTag == 10) {
        vcDate.requiredSelectedTime = self.commonAddressViewModel.startTime;
    }else{
        vcDate.requiredSelectedTime = self.commonAddressViewModel.endTime;
    }
    
    WEAKSELF
    vcDate.cancelBlock = ^(){
        [weakSelf dismissWtViewControllerAnimated:YES];
    };
    vcDate.ensureBlock = ^(NSMutableArray *msg){
        [weakSelf dismissWtViewControllerAnimated:YES];
        
        if (weakSelf.commonAddressViewModel.addressTimeTag == 10) {
            weakSelf.commonAddressViewModel.startTime = [NSString stringWithFormat:@"%@:%@",[msg firstObject],[msg lastObject]];
        }else{
            weakSelf.commonAddressViewModel.endTime = [NSString stringWithFormat:@"%@:%@",[msg firstObject],[msg lastObject]];
        }
        [weakSelf.tableView reloadData];
    };
    
    [self presentWtViewController:vcDate animated:YES];
}

#pragma mark ---- 保存 ----
- (IBAction)saveAction:(id)sender
{
    [self.commonAddressViewModel addAddess:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            [SVProgressHUD showSuccessWithStatus:@"保存成功"];
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
